//import pathToRegexp from 'path-to-regexp';
import { favgoodsOne,cartSettlement,cartListApi,recommend, changeCartNum, checkCart, delCart, syncCart, couponlistApi, receiveCouponApi, checkAllCart, delCheckCart, favgoods, getCartSpecApi, getSpecIdApi, putSpecIdApi } from './api';
import { Toast } from 'antd-mobile';

export default {
  namespace: 'cart',
  state: {
    cartIds: [],
    couponMessage:'',
    select: [],
    editStatus: true,
    couponList: [],
    discountedPrice:0.00,
    isAllCheck:0,
    totlePrice:0.00
  },
  reducers: {
    save(state, { data }) {
      return { ...state, ...data };
    },

    changeEditStatus(state) {
      return { ...state, editStatus: !state.editStatus };
    },

    changeCartNums(state, { shopIndex, goodsIndex, count }) {
       const cartIds = state.cartIds;
       cartIds[shopIndex].goodslist[goodsIndex].goodsNum = count;

      return { ...state, cartIds };
    },

    changeCartGoodsSelect(state, { shopIndex, goodsIndex, isCheck }) {
        const cartIds = state.cartIds;
        cartIds[shopIndex].goodslist[goodsIndex].isCheck = isCheck;
        return { ...state, cartIds };
    },

    changeCartStoreSelect(state, { shopIndex, isCheck }) {
        const cartIds = state.cartIds;
        const newCartIds = cartIds.map((v,i,a)=>{
           if(shopIndex==i){
               const goodslist = v.goodslist.map((vv,ii,aa)=>{
                 return { ...vv, isCheck:isCheck }
               });
               return { ...v , goodslist:goodslist}
           }else {
             return v
           }
        });
       // cartIds[shopIndex].goodslist[goodsIndex].isCheck = isCheck;
        return { ...state, cartIds:newCartIds };
    },

  changeCartStoreAllSelect(state, { isCheck }) {
    const cartIds = state.cartIds;
    const newCartIds = cartIds.map((v,i,a)=>{

          const goodslist = v.goodslist.map((vv,ii,aa)=>{
              return { ...vv, isCheck:isCheck }
          });

          return { ...v , goodslist:goodslist}
    });

    return { ...state, cartIds:newCartIds };
  },

    changeCartALLSelect(state, { isCheck }) {
        const cartIds = state.cartIds;
        const newCartIds = cartIds.map((v,i,a)=>{
            const goodslist = v.goodslist.map((vv,ii,aa)=>{
                return { ...vv, isCheck:isCheck }
            });
            return { ...v , goodslist:goodslist}
        });
        // cartIds[shopIndex].goodslist[goodsIndex].isCheck = isCheck;
        return { ...state, cartIds:newCartIds };
    },

    deleteCart(state, { shopIndex, goodsIndex }){

        let newCartIds = state.cartIds.map((v,i,a)=>{

            if(shopIndex==i){
              const goodslist = v.goodslist.filter((vv,ii,aa)=>{
                    return ii != goodsIndex
                });
                return { ...v , goodslist:goodslist}
            } else {
              return v
            }

           // return goodslist.length > 0
        });

        newCartIds = newCartIds.filter((v,i,a)=>{
           return v.goodslist.length > 0
        })

        return { ...state, cartIds:newCartIds };
    },

      receiveCoupon(state, { id }){

          const couponList =state.couponList.map((v,i,a)=>{
              if(v.id==id){
                  return { ...v, isGet :1 }
              }else {
                  return v
              }
          })

          return { ...state, couponList };
      },

  },
  effects: {
    *cartListEFF({ }, { put, call }) {
     // Toast.loading();
      const result = yield call(cartListApi);
      if (result.code == 200) {
          Toast.hide();
        yield put({ type: 'save', data: { ... result.ret } });
       // Toast.success(result.msg, 1.5);
      } else {
        // Toast.fail(result.msg, 1.5);
      }
    },
    *cartSettlementEFF({ }, { put, call }) {
         // Toast.loading();
          const result = yield call(cartSettlement);
          return result
        },


    *changeCartNumEFF({ shopIndex, goodsIndex, obj }, { put, call }) {
          Toast.loading();
          const result = yield call(changeCartNum, obj);
          if (result.code == 200 && result.ret.result == 1) {
         //     Toast.hide();
             // yield put({ type: 'changeCartNums', shopIndex, goodsIndex, count:obj.count });
              //Toast.success(result.msg, 1.5);
              yield put({ type: 'cartListEFF' });
          } else if (result.code == 200) {
              Toast.fail(result.msg, 1);
          } else {
              return false;
          }
    },

    *checkCartEFF({ goodsIndex, shopIndex, isCheck, obj }, {  put, call }) {
        Toast.loading();

        const result = yield call(checkCart, obj);
        if (result.code == 200) {
        //  Toast.hide();
           // yield put({ type: 'changeCartGoodsSelect', shopIndex, goodsIndex, isCheck });
            yield put({ type: 'cartListEFF' });
            //Toast.success(result.msg, 1.5);
        } else {

        }
    },

    *checkShopCartEFF({  shopIndex, isCheck, obj }, {  put, call }) {
        Toast.loading();

        const result = yield call(checkAllCart, obj);
        if (result.code == 200) {
        //  Toast.hide();
           // yield put({ type: 'changeCartStoreSelect', shopIndex, isCheck });
            yield put({ type: 'cartListEFF' });
            //Toast.success(result.msg, 1.5);
        } else {

        }
    },

    *checkShopAllCartEFF({ isCheck, obj }, {  put, call }) {
        Toast.loading();
        const result = yield call(checkAllCart, obj);
        if (result.code == 200) {
        //    Toast.hide();
            //yield put({ type: 'changeCartStoreAllSelect', isCheck });
            yield put({ type: 'cartListEFF' });

            //Toast.success(result.msg, 1.5);
        } else {

        }
    },

    *deleteCartEFF({ shopIndex, goodsIndex, obj }, { put, call }) {
      Toast.loading();
      const result = yield call(delCart, obj);
      if (result.code == 200) {
       // Toast.hide();
          yield put({ type: 'cartListEFF' });
     //   yield put({ type: 'deleteCart', shopIndex, goodsIndex });
       // Toast.success(result.msg, 1.5);
      } else {

      }
    },

    *delCheckCartEFF({  }, { put, call }) {
        Toast.loading();
        const result = yield call(delCheckCart);
        if (result.code == 200) {
        //  Toast.hide();
          yield put({ type: 'cartListEFF' });
        } else {

        }
    },

      *favgoodsEFF({  }, { put, call }) {
          Toast.loading();
          const result = yield call(favgoods);
          if (result.code == 200) {
              Toast.success(result.msg, 1.5);
             // yield put({ type: 'cartListEFF' });
          } else {

          }
      },
      *favgoodsOneEFF({ obj }, { put, call }) {
          Toast.loading();
          const result = yield call(favgoodsOne,obj);
          if (result.code == 200) {
              Toast.success(result.msg, 1.5);
              yield put({ type: 'cartListEFF' });
          } else {

          }
      },

    *syncCartEFF({ }, { put, call }) {
        Toast.loading();
        const result = yield call(syncCart);
        if (result.code == 200) {

        } else {

        }
    },

      *couponlistEFF({ obj }, { put, call }) {
          Toast.loading();
          const result = yield call(couponlistApi, obj);
          if (result.code == 200) {
              Toast.hide();
              yield put({ type: 'save', data: { couponList: result.ret.list || [] } });
          } else {

          }
      },

      *receiveCouponEFF({ obj }, { put, call }) {
          Toast.loading();
          const result = yield call(receiveCouponApi, obj);
          if (result.code == 200) {
              Toast.hide();
              yield put({ type:'receiveCoupon', id:obj.id  })
          } else {

          }
      },
      *recommendEFF({ }, { put, call }) {
          Toast.loading('加载中...', 3);
          const result = yield call(recommend);
          if (result.code == 200) {
              yield put({ type: 'save', data: result.ret });
              // Toast.hide();
          } else {

          }
      },
     *getSpecIdEFF({ obj }, {put, call}) {
         Toast.loading('加载中...', 3);
        const result = yield call(getSpecIdApi, obj);
        if (result.code == 200) {
            Toast.hide();
        } else {

        };
        Toast.hide();
        return result;
     },
     *putSpecIdEFF({ obj }, {put, call}) {
         Toast.loading('加载中...', 3);
           const result = yield call(putSpecIdApi, obj);
           if (result.code == 200) {
               Toast.hide();
           } else {

           };
           Toast.hide();
           return result;
      },
      *getCartSpecEFF({ obj }, {put, call}) {
          Toast.loading('加载中...', 3);
         const result = yield call(getCartSpecApi, obj);
         if (result.code == 200) {
             Toast.hide();
         } else {

         };
         Toast.hide();
         return result;
      },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        // console.log(pathname)
        if(pathname=='/cart'){
          dispatch({ type:'cartListEFF' }).then(()=>{
              dispatch({ type: 'recommendEFF' });
          });;
        //  dispatch({ type:'syncCartEFF' });
        }
      });
    },
  },
};
